package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gcgl.domain.YyProjectSqkp;
import com.ruoyi.gcgl.mapper.GcProjectSqkpMapper;
import com.ruoyi.gcgl.mapper.YyProjectSqkpMapper;
import com.ruoyi.gcgl.service.IGcProjectSqkpService;
import com.ruoyi.gcgl.service.IYyProjectSqkpService;

/**
 * 申请开票Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-11
 */
@Service
public class YyProjectSqkpServiceImpl implements IYyProjectSqkpService 
{
    @Autowired
    private YyProjectSqkpMapper gcProjectSqkpMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询申请开票
     * 
     * @param id 申请开票主键
     * @return 申请开票
     */
    @Override
    public YyProjectSqkp selectGcProjectSqkpById(Long id)
    {
        return gcProjectSqkpMapper.selectGcProjectSqkpById(id);
    }

    /**
     * 查询申请开票列表
     * 
     * @param gcProjectSqkp 申请开票
     * @return 申请开票
     */
    @Override
    public List<YyProjectSqkp> selectGcProjectSqkpList(YyProjectSqkp gcProjectSqkp)
    {
        return gcProjectSqkpMapper.selectGcProjectSqkpList(gcProjectSqkp);
    }

    /**
     * 新增申请开票
     * 
     * @param gcProjectSqkp 申请开票
     * @return 结果
     */
    @Override
    public int insertGcProjectSqkp(YyProjectSqkp gcProjectSqkp)
    {
    	int rows = gcProjectSqkpMapper.insertGcProjectSqkp(gcProjectSqkp);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(gcProjectSqkp.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", gcProjectSqkp.getOwner());
        variables.put("level1", gcProjectSqkp.getSwyy());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("yykaipiaoshenqing", String.valueOf(gcProjectSqkp.getId()), variables);
    	
    	YyProjectSqkp obj = new YyProjectSqkp();
    	obj.setId(gcProjectSqkp.getId());
    	obj.setProcessInstanceId(pi.getProcessInstanceId());
    	gcProjectSqkpMapper.updateGcProjectSqkp(obj);
    	
        return rows;
    }

    /**
     * 修改申请开票
     * 
     * @param gcProjectSqkp 申请开票
     * @return 结果
     */
    @Override
    public int updateGcProjectSqkp(YyProjectSqkp gcProjectSqkp)
    {
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(gcProjectSqkp.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", gcProjectSqkp.getOwner());
//        variables.put("level1", gcProjectSqkp.getApprovename());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjkaipiaoshenqing", String.valueOf(gcProjectSqkp.getId()), variables);
//        gcProjectSqkp.setProcessInstanceId(pi.getProcessInstanceId());
        
        return gcProjectSqkpMapper.updateGcProjectSqkp(gcProjectSqkp);
    }

    /**
     * 批量删除申请开票
     * 
     * @param ids 需要删除的申请开票主键
     * @return 结果
     */
    @Override
    public int deleteGcProjectSqkpByIds(String ids)
    {
        return gcProjectSqkpMapper.deleteGcProjectSqkpByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除申请开票信息
     * 
     * @param id 申请开票主键
     * @return 结果
     */
    @Override
    public int deleteGcProjectSqkpById(Long id)
    {
        return gcProjectSqkpMapper.deleteGcProjectSqkpById(id);
    }

	@Override
	public int updateGcProjectSqkp4check(YyProjectSqkp gcProjectSqkp) {
		// TODO Auto-generated method stub
		return gcProjectSqkpMapper.updateGcProjectSqkp(gcProjectSqkp);
	}
}
